#include "SoPhuc.h"


SoPhuc::SoPhuc()
{
	pThuc = new double;
	pAo = new double;
	*pThuc = 0;
	*pAo = 0;
}

SoPhuc::SoPhuc(const SoPhuc &objSoPhuc)
{
	pThuc = new double;
	pAo = new double;
	*pThuc = *objSoPhuc.pThuc;
	*pAo = *objSoPhuc.pAo;
}

SoPhuc::~SoPhuc()
{
	delete pThuc;
	delete pAo;
}


double SoPhuc::GetThuc() const
{
	return *pThuc;
}

double SoPhuc::GetAo() const
{
	return *pAo;
}

int SoPhuc::SetThuc(double praThuc)
{
	*pThuc = praThuc;
	return 1;
}

int SoPhuc::SetAo(double praAo)
{
	*pAo = praAo;
	return 1;
}

void SoPhuc::operator = (const SoPhuc &praSoPhuc)
{
	*pThuc = *praSoPhuc.pThuc;
	*pAo = *praSoPhuc.pAo;
}
	
void SoPhuc::Nhap()
{
	cout << "Nhap phan thuc: ";
	cin >> *pThuc;
	cout << "Nhap phan ao: ";
	cin >> *pAo;
}

void SoPhuc::Xuat() const
{
	if (*pThuc != 0)
	{
		cout << *pThuc;
		if (*pAo > 0) cout << " + " << *pAo << "i";
		else if (*pAo < 0) cout << " - " << (-1)* *pAo << "i";
	}
	if (*pThuc == 0)
	{
		cout << *pAo << "i";
	}
}

SoPhuc SoPhuc::operator +(const SoPhuc &praSoPhuc) const
{
	SoPhuc varTemp;
	*varTemp.pThuc = *pThuc + praSoPhuc.GetThuc();
	*varTemp.pAo = *pAo + praSoPhuc.GetAo();

	return	varTemp;
}

SoPhuc SoPhuc::operator -(const SoPhuc &praSoPhuc) const
{
	SoPhuc varTemp;
	*varTemp.pThuc = *pThuc - praSoPhuc.GetThuc();
	*varTemp.pAo = *pAo - praSoPhuc.GetAo();

	return varTemp;
}

SoPhuc SoPhuc::operator *(const SoPhuc &praSoPhuc) const
{
	SoPhuc varTemp;
	*varTemp.pThuc = *pThuc * praSoPhuc.GetThuc() - *pAo * praSoPhuc.GetAo();
	*varTemp.pAo = *pThuc * praSoPhuc.GetAo() + *pAo * praSoPhuc.GetThuc();

	return varTemp;
}

SoPhuc SoPhuc::operator /(const SoPhuc &praSoPhuc) const
{
	SoPhuc varTemp;
	double varThuc = *pThuc;
	double varMau = praSoPhuc.GetThuc() * praSoPhuc.GetThuc() + praSoPhuc.GetAo() * praSoPhuc.GetAo();
	*varTemp.pThuc = (*pThuc * praSoPhuc.GetThuc() + *pAo * praSoPhuc.GetAo()) / varMau;
	*varTemp.pAo = (praSoPhuc.GetThuc() * *pAo - varThuc * praSoPhuc.GetAo()) / varMau;

	return varTemp;
}

void SoPhuc::operator +=(const SoPhuc &praSoPhuc)
{
	*pThuc = *pThuc + praSoPhuc.GetThuc();
	*pAo = *pAo + praSoPhuc.GetAo();
}

void SoPhuc::operator -=(const SoPhuc &praSoPhuc)
{
	*pThuc = *pThuc - praSoPhuc.GetThuc();
	*pAo = *pAo - praSoPhuc.GetAo();
}

void SoPhuc::operator *=(const SoPhuc &praSoPhuc)
{
	double varThuc = *pThuc;
	*pThuc = *pThuc * praSoPhuc.GetThuc() - *pAo * praSoPhuc.GetAo();
	*pAo = varThuc * praSoPhuc.GetAo() + *pAo * praSoPhuc.GetThuc();
}

void SoPhuc::operator /=(const SoPhuc &praSoPhuc)
{
	double varThuc = *pThuc;
	double varMau = praSoPhuc.GetThuc() * praSoPhuc.GetThuc() + praSoPhuc.GetAo() * praSoPhuc.GetAo();
	*pThuc = (*pThuc * praSoPhuc.GetThuc() + *pAo * praSoPhuc.GetAo()) / varMau;
	*pAo = (praSoPhuc.GetThuc() * *pAo - varThuc * praSoPhuc.GetAo()) / varMau;
}